import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'auditLogM',

  state: {
    loading: true,
    tenantData: [], //租户列表
    operateTypeList: [], //操作类型列表
    userList: [], //用户列表
  },
  effects: {
    // 获取用户列表
    *getUserList({ payload }, { call, put }) {
      const data = yield call(api.getUserList, payload);
      let temp = [];
      data?.data?.forEach((val) => {
        temp.push({ value: val.name, title: val.name });
      });
      if (data && data.success == true) {
        yield put({
          type: 'save',
          payload: {
            userList: temp,
          },
        });
      } else {
        message.error(`获取用户列表失败，失败原因：${data?.mesg}`);
        console.error(`获取用户列表失败，失败原因：${data?.mesg}`, '接口名称： findAll 接口：portfolio-server/user/findAll');
      }
    },
    // 获取租户列表
    *SearchTenant({ payload }, { call, put }) {
      const data = yield call(api.userManageNew.SearchTenant, payload);
      let temp = [];
      data?.data?.dataSource.forEach((val) => {
        temp.push({ value: val.code, title: val.name });
      });
      if (data && data.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tenantData: temp,
            loading: false,
          },
        });
      } else {
        message.error(`获取租户信息失败，失败原因：${data?.msg}`);
        console.error(`获取租户信息失败，失败原因：${data?.msg}`, '接口名称： page-query 接口：portfolio-server/tenant/page-query');
      }
    },
    //获取日志表格数据
    *getLogTableData({ payload, callback }, { call, put }) {
      const response = yield call(api.auditLog.getLogTableData, payload);
      if (response && response.code === 0) {
        callback('success', response.data);
      } else {
        callback('fail');
        message.error(`获取日志列表数据，失败原因：${response?.msg}`);
        console.error(`获取日志列表数据，失败原因：${response?.msg}`, '接口名称： page-query 接口：portfolio-server/audit-log/page-query');
      }
    },
    // 根据Id获取操作内容
    *getLogById({ payload, callback }, { call, put }) {
      const response = yield call(api.auditLog.getLogById, payload);
      if (response && response.code === 0) {
        callback('success', response?.data?.dataSource);
      } else {
        callback('fail');
        message.error(`获取用户操作数据失败，失败原因：${response?.msg}`);
        console.error(`获取用户操作数据失败，失败原因：${response?.msg}`, '接口名称： detail 接口：portfolio-server/audit-log/detail');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
